/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.modules.activity.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import co.yixiang.api.YshopException;
import co.yixiang.common.service.impl.BaseServiceImpl;
import co.yixiang.common.utils.QueryHelpPlus;
import co.yixiang.constant.ShopConstants;
import co.yixiang.dozer.service.IGenerator;
import co.yixiang.enums.CouponEnum;
import co.yixiang.modules.activity.domain.YxStoreCoupon;
import co.yixiang.modules.activity.domain.YxStoreCouponIssue;
import co.yixiang.modules.activity.domain.YxStoreCouponUser;
import co.yixiang.modules.activity.service.YxStoreCouponService;
import co.yixiang.modules.activity.service.YxStoreCouponUserService;
import co.yixiang.modules.activity.service.dto.YxStoreCouponUserDto;
import co.yixiang.modules.activity.service.dto.YxStoreCouponUserQueryCriteria;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponIssueMapper;
import co.yixiang.modules.activity.service.mapper.YxStoreCouponUserMapper;
import co.yixiang.modules.activity.vo.StoreCouponUserVo;
import co.yixiang.modules.activity.vo.YxStoreCouponUserQueryVo;
import co.yixiang.modules.cart.service.YxStoreCartService;
import co.yixiang.modules.cart.vo.YxStoreCartQueryVo;
import co.yixiang.modules.product.domain.YxStoreProduct;
import co.yixiang.modules.product.service.YxStoreProductService;
import co.yixiang.modules.user.domain.YxUser;
import co.yixiang.modules.user.service.YxUserService;
import co.yixiang.utils.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


/**
 * @author hupeng
 * @date 2020-05-13
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreCouponUserServiceImpl extends BaseServiceImpl<YxStoreCouponUserMapper, YxStoreCouponUser> implements YxStoreCouponUserService {

    @Autowired
    private IGenerator generator;


    @Autowired
    private YxStoreCouponUserMapper yxStoreCouponUserMapper;

    @Autowired
    private YxStoreCouponIssueMapper yxStoreCouponIssueMapper;

    @Autowired
    private YxUserService userService;
    @Autowired
    private YxStoreCouponService storeCouponService;

    @Autowired
    private YxStoreCartService yxStoreCartService;
    @Autowired
    private YxStoreProductService productService;

    /**
     * 获取当前用户优惠券数量
     *
     * @param uid uid
     * @return int
     */
    @Override
    public Long getUserValidCouponCount(Long uid) {
        this.checkInvalidCoupon();
        return this.lambdaQuery()
                .eq(YxStoreCouponUser::getStatus, CouponEnum.STATUS_0.getValue())
                .eq(YxStoreCouponUser::getUid, uid)
                .count();
    }

    /**
     * 获取满足条件的可用优惠券
     *
     * @param cartIds 购物车ids
     * @return list
     */
    @Override
    public List<StoreCouponUserVo> beUsableCouponList(Long uid, String cartIds) {

        Map<String, Object> cartGroup = yxStoreCartService.getUserProductCartList(uid,
                cartIds, ShopConstants.YSHOP_ONE_NUM);

        List<YxStoreCartQueryVo> cartInfo = (List<YxStoreCartQueryVo>) cartGroup.get("valid");

        BigDecimal sumPrice = BigDecimal.ZERO;//订单总金额
        Integer goodsNum = 0;//订单商品总数
        Map<Long, Integer> cartGoodsNumMap = new HashMap<>();
        for (YxStoreCartQueryVo storeCart : cartInfo) {
            sumPrice = NumberUtil.add(sumPrice, NumberUtil.mul(storeCart.getCartNum(), storeCart.getTruePrice()));
            goodsNum = goodsNum + storeCart.getCartNum();
            cartGoodsNumMap.put(storeCart.getProductId(), storeCart.getCartNum());
        }

        List<Long> productIds = cartInfo.stream()
                .map(YxStoreCartQueryVo::getProductId)
                .collect(Collectors.toList());
        return this.getUsableUserCouponList(uid, sumPrice.doubleValue(), goodsNum, productIds, cartGoodsNumMap);
    }

    /**
     * 根据订单商品总数或总金额，获取下单时候满足的优惠券
     *
     * @param uid
     * @param sumPrice
     * @param goodsNum
     * @param productIds
     * @param cartGoodsNumMap
     * @return
     */
    @Override
    public List<StoreCouponUserVo> getUsableUserCouponList(Long uid, double sumPrice, Integer goodsNum, List<Long> productIds, Map<Long, Integer> cartGoodsNumMap) {
        /**
         * 知道订单商品总数或总金额吗，用户ID,商品ID集合
         * 先查询当前用户未使用、未删除、未过期的所有用户优惠券列表
         * 遍历，判断优惠券类型
         * 通用券，按优惠券面值限制，订单总金额大于等于最低消费金额且订单总金额大于等于优惠券面额
         * 通用券，按商品总数限制，限制商品总数大于等于订单商品总数
         * 商品券，按优惠券面值限制，订单的商品和优惠券商品完全一样，且订单总金额大于等于最低消费金额且订单总金额大于等于优惠券面额
         * 商品券，按不同商品的最低购买数量限制，订单的商品和优惠券商品完全一样，且每个优惠券商品的最低购买数量大于等于订单对应商品总数
         *
         */
        Date now = new Date();
        List<StoreCouponUserVo> storeCouponUsers = yxStoreCouponUserMapper.selectUsableCouponListByUserId(now, uid);
        List<StoreCouponUserVo> storeUsableCouponUserList = new ArrayList<>();
        for (StoreCouponUserVo storeCouponUser : storeCouponUsers) {
            if (storeCouponUser.getCouponPrice().compareTo(sumPrice) < 0) {//优惠券面额小于订单总金额，则跳过
                continue;
            }
            //优惠券类型 0通用券 1商品券
            if (CouponEnum.TYPE_0.getValue().equals(storeCouponUser.getType())) {
                //限制类型，1限制金额2限制商品数量3限制商品分类
                if (storeCouponUser.getLimitType() == CouponEnum.LIMIT_TYPE_1.getValue()) {
                    if (storeCouponUser.getUseMinPrice().compareTo(sumPrice) > 0) {//订单总金额小于最低消费金额，则跳过
                        continue;
                    }
                } else if (storeCouponUser.getLimitType() == CouponEnum.LIMIT_TYPE_2.getValue()) {
                    if (storeCouponUser.getLimitGoodsNum() > goodsNum) {//限制商品总数大于订单商品总数，则跳过
                        continue;
                    }
                }
            } else if (CouponEnum.TYPE_1.getValue().equals(storeCouponUser.getType())) {
                //存在订单商品和优惠券商品不一致则跳过
                //限制类型，1限制金额2限制商品数量3限制商品分类
                if (storeCouponUser.getLimitType() == CouponEnum.LIMIT_TYPE_1.getValue()) {
                    if (storeCouponUser.getUseMinPrice().compareTo(sumPrice) > 0) {//订单总金额小于最低消费金额，则跳过
                        continue;
                    }
                } else if (storeCouponUser.getLimitType() == CouponEnum.LIMIT_TYPE_2.getValue()) {
                    //商品券，按不同商品的最低购买数量限制，订单的商品和优惠券商品存在不一样，则跳过，或每个优惠券商品的最低购买数量大于对应商品总数，则跳过
//                    [{"id": 5, "storeName": "广告版定制", "limitGoodsNum": 1}, {"id": 6, "storeName": "印刷各种印刷", "limitGoodsNum": 1}]
                    String productIdJson = storeCouponUser.getProductIdJson();
                    Boolean isGoodsSale = true;
                    if (StrUtil.isNotEmpty(productIdJson)) {
                        JSONArray productIdArray = new JSONArray(productIdJson);
                        for (int i = 0; i < productIdArray.size(); i++) {
                            JSONObject productIdJSONObject = productIdArray.getJSONObject(i);
                            Long productId = productIdJSONObject.getLong("id");
                            Integer cartGoodsNum = cartGoodsNumMap.get(productId);
                            if (cartGoodsNum != null) {
                                Integer limitGoodsNum = productIdJSONObject.getInt("limitGoodsNum");
                                if (limitGoodsNum > cartGoodsNum) {
                                    isGoodsSale = false;
                                    continue;
                                }
                            } else {
                                isGoodsSale = false;
                                continue;
                            }
                        }
                    }
                    if (!isGoodsSale) {
                        continue;
                    }
                    //判断限制商品分类的数量
                    Integer isLimitCate = storeCouponUser.getIsLimitCate();
                    String cateIds = storeCouponUser.getCateIds();
                    Integer limitGoodCateNum = storeCouponUser.getLimitGoodCateNum();
                    Boolean isGoodsCateSale = true;
                    if (isLimitCate == 1) {
                        List<Integer> cateList = Arrays.stream(cateIds.split(","))
                                .map(Integer::parseInt)
                                .collect(Collectors.toList());
                        //统计商品对应分类的数量
                        Map<Integer, Integer> countProductCateMap = this.countProductCategories(productIds);
                        for (Integer cateId : cateList) {
                            Integer productCateNum = countProductCateMap.get(cateId);
                            if (productCateNum != null){
                                if (limitGoodCateNum > productCateNum) {
                                    isGoodsCateSale = false;
                                    continue;
                                }
                            }else{
                                isGoodsCateSale = false;
                                continue;
                            }
                        }

                    }
                    if (!isGoodsCateSale) {
                        continue;
                    }

                }else if (storeCouponUser.getLimitType() == CouponEnum.LIMIT_TYPE_3.getValue()) {
                    //商品券，按商品分类限制
                    //判断选择的商品的分类是否在优惠券配置的商品分类里面，如果有一个不在则跳过
                    String cateIds = storeCouponUser.getCateIds();
                    List<Integer> cateList = Arrays.stream(cateIds.split(","))
                            .map(Integer::parseInt)
                            .collect(Collectors.toList());
                    Map couponCateIdMap = new HashMap();
                    for (Integer couponCateId : cateList) {
                        couponCateIdMap.put(couponCateId,couponCateId);
                    }
                    Boolean isHasGoodsCate = true;
                    for (Long productId : productIds) {
                        YxStoreProduct storeProduct = productService.getById(productId);
                        if (storeProduct != null && StrUtil.isNotEmpty(storeProduct.getCateId())) {
                            Integer productCateId = Integer.valueOf(storeProduct.getCateId());
                            if(couponCateIdMap.get(productCateId) == null){
                                isHasGoodsCate = false;
                                break;
                            }
                        }
                    }
                    if (!isHasGoodsCate) {
                        continue;
                    }
                }
            }
            storeUsableCouponUserList.add(storeCouponUser);
        }
        return storeUsableCouponUserList;
    }

    public Map<Integer, Integer> countProductCategories(List<Long> productIds) {
        Map<Integer, Integer> productCategoryCounts = new HashMap<>();

        for (Long productId : productIds) {
            YxStoreProduct storeProduct = productService.getById(productId);
            if (storeProduct != null && StrUtil.isNotEmpty(storeProduct.getCateId())) {
                int cateId = Integer.valueOf(storeProduct.getCateId());
                int count = productCategoryCounts.getOrDefault(cateId, 0) + 1;
                productCategoryCounts.put(cateId, count);
            }
        }
        return productCategoryCounts;
    }


    /**
     * 获取用户优惠券
     *
     * @param id  优惠券id
     * @param uid 用户id
     * @return YxStoreCouponUser
     */
    @Override
    public YxStoreCouponUser getCoupon(Integer id, Long uid) {
        return this.lambdaQuery()
                .eq(YxStoreCouponUser::getIsFail, CouponEnum.FALI_0.getValue())
                .eq(YxStoreCouponUser::getStatus, CouponEnum.STATUS_0.getValue())
                .eq(YxStoreCouponUser::getUid, uid)
                .eq(YxStoreCouponUser::getId, id)
                .one();
    }

    @Override
    public void useCoupon(int id) {
        YxStoreCouponUser couponUser = new YxStoreCouponUser();
        couponUser.setId((long) id);
        couponUser.setStatus(1);
        couponUser.setUseTime(new Date());
        yxStoreCouponUserMapper.updateById(couponUser);
    }


    /**
     * 获取用户优惠券
     *
     * @param uid uid
     * @return list
     */
    @Override
    public List<YxStoreCouponUserQueryVo> getUserCoupon(Long uid) {
        //this.checkInvalidCoupon();
        List<YxStoreCouponUser> storeCouponUsers = yxStoreCouponUserMapper
                .selectList(Wrappers.<YxStoreCouponUser>lambdaQuery()
                        .eq(YxStoreCouponUser::getUid, uid));
        List<YxStoreCouponUserQueryVo> storeCouponUserQueryVoList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (YxStoreCouponUser couponUser : storeCouponUsers) {
            YxStoreCouponUserQueryVo queryVo = generator.convert(couponUser, YxStoreCouponUserQueryVo.class);
            if (couponUser.getIsFail() == 1) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已失效");
            } else if (couponUser.getStatus() == 1) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已使用");
            } else if (couponUser.getStatus() == 2) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已过期");
            } else if (couponUser.getCreateTime().getTime() > nowTime || couponUser.getEndTime().getTime() < nowTime) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已过期");
            } else {
                queryVo.set_type(CouponEnum.USE_1.getValue());
                queryVo.set_msg("可使用");
            }
            YxStoreCoupon storeCoupon = storeCouponService.getById(couponUser.getCid());
            if (storeCoupon != null) {
                queryVo.setCouponType(storeCoupon.getType());
                queryVo.setCouponTitle(storeCoupon.getTitle());
                queryVo.setCouponPrice(storeCoupon.getCouponPrice());
                queryVo.setUseMinPrice(storeCoupon.getUseMinPrice());
            }
            //发布的优惠券禁用则看不到
            LambdaQueryWrapper<YxStoreCouponIssue> couponIssueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            couponIssueLambdaQueryWrapper.eq(YxStoreCouponIssue::getCid, storeCoupon.getId());
            couponIssueLambdaQueryWrapper.eq(YxStoreCouponIssue::getStatus, 0);
            if (yxStoreCouponIssueMapper.selectCount(couponIssueLambdaQueryWrapper) > 0) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已失效");
            }
            storeCouponUserQueryVoList.add(queryVo);
        }
        return storeCouponUserQueryVoList;
    }

    /**
     * 添加优惠券记录
     *
     * @param uid 用户id
     * @param cid 优惠券id
     */
    @Override
    public void addUserCoupon(Long uid, Integer cid, Integer couponIssueId, String getType) {
        YxStoreCoupon storeCoupon = storeCouponService.getById(cid);
        if (storeCoupon == null) {
            throw new YshopException("优惠劵不存在");
        }

        Date now = new Date();

        Date endTime = DateUtil.offsetDay(now, storeCoupon.getCouponTime());

        YxUser yxUser = userService.getById(uid);
        YxStoreCouponUser storeCouponUser = YxStoreCouponUser.builder()
                .cid(storeCoupon.getId())
                .couponIssueId(couponIssueId)
                .uid(uid)
                .username(yxUser.getUsername())
                .couponPrice(storeCoupon.getCouponPrice())
                .couponTitle(storeCoupon.getTitle())
                .useMinPrice(storeCoupon.getUseMinPrice())
                .endTime(endTime)
                .type(getType)
                .build();

        this.save(storeCouponUser);

    }


    /**
     * 判断两个list是否有相同值
     *
     * @param list1 list
     * @param list2 list
     * @return boolean
     */
    private boolean isSame(List<String> list1, List<String> list2) {
        if (list2.isEmpty()) {
            return true;
        }
        list1 = new ArrayList<>(list1);
        list2 = new ArrayList<>(list2);
        list1.addAll(list2);
        int total = list1.size();

        List<String> newList = new ArrayList<>(new HashSet<>(list1));

        int newTotal = newList.size();


        return total > newTotal;
    }


    /**
     * 检查优惠券状态
     */
    private void checkInvalidCoupon() {
        Date nowTime = new Date();
        LambdaQueryWrapper<YxStoreCouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.lt(YxStoreCouponUser::getEndTime, nowTime)
                .eq(YxStoreCouponUser::getStatus, CouponEnum.STATUS_0.getValue());
        YxStoreCouponUser couponUser = new YxStoreCouponUser();
        couponUser.setStatus(CouponEnum.STATUS_2.getValue());
        yxStoreCouponUserMapper.update(couponUser, wrapper);

    }


    //=========================================================================================================//

    @Override
    //@Cacheable
    public Map<String, Object> queryAll(YxStoreCouponUserQueryCriteria criteria, Pageable pageable) {
        PageInfo<YxStoreCouponUser> page = new PageInfo<>();
        List<YxStoreCouponUserDto> storeOrderDTOS = new ArrayList<>();
        getPage(pageable);
        Map<String, Object> map = new LinkedHashMap<>(2);
        if (StrUtil.isNotEmpty(criteria.getCouponTitle())) {
            LambdaQueryWrapper<YxStoreCoupon> storeCouponLambdaQueryWrapper = new LambdaQueryWrapper<>();
            storeCouponLambdaQueryWrapper.like(YxStoreCoupon::getTitle, criteria.getCouponTitle());
            List<YxStoreCoupon> storeCouponList = storeCouponService.list(storeCouponLambdaQueryWrapper);
            if (storeCouponList.size() > 0) {
                // 使用流操作提取ID并转换为List<Integer>
                List<Integer> cidList = storeCouponList.stream()
                        .map(YxStoreCoupon::getId)  // 使用方法引用提取ID
                        .collect(Collectors.toList());  // 收集到新的列表中
                if (cidList.size() > 0) {
                    criteria.setCid(cidList);
                }
            } else {
                map.put("content", storeOrderDTOS);
                map.put("totalElements", page.getTotal());
                return map;
            }

        }
        if (StrUtil.isNotEmpty(criteria.getUsername())) {
            List<YxUser> userList = userService.list(Wrappers.<YxUser>lambdaQuery()
                    .like(YxUser::getUsername, criteria.getUsername()).orderByDesc(YxUser::getCreateTime));
            if (userList.size() > 0) {
                List<Long> uidList = userList.stream()
                        .map(YxUser::getUid) // 使用方法引用提取id
                        .collect(Collectors.toList()); // 收集结果为List<Long>
                if (uidList.size() > 0) {
                    criteria.setUid(uidList);
                }
            } else {
                map.put("content", storeOrderDTOS);
                map.put("totalElements", page.getTotal());
                return map;
            }
        }
        if (StrUtil.isNotEmpty(criteria.getShopName())) {
            List<YxUser> userList = userService.list(Wrappers.<YxUser>lambdaQuery()
                    .like(YxUser::getShopName, criteria.getShopName()).orderByDesc(YxUser::getCreateTime));
            if (userList.size() > 0) {
                List<Long> uidList = userList.stream()
                        .map(YxUser::getUid) // 使用方法引用提取id
                        .collect(Collectors.toList()); // 收集结果为List<Long>
                if (uidList.size() > 0) {
                    criteria.setUid(uidList);
                }
            } else {
                map.put("content", storeOrderDTOS);
                map.put("totalElements", page.getTotal());
                return map;
            }
        }
        page = new PageInfo<>(queryAll(criteria));
        storeOrderDTOS = generator.convert(page.getList(), YxStoreCouponUserDto.class);
        for (YxStoreCouponUserDto couponUserDTO : storeOrderDTOS) {
            YxStoreCoupon storeCoupon = storeCouponService.getById(couponUserDTO.getCid());
            if (storeCoupon != null) {
                couponUserDTO.setCouponTitle(storeCoupon.getTitle());
                couponUserDTO.setCouponPrice(storeCoupon.getCouponPrice());
            }
            YxUser yxUser = userService.getOne(new LambdaQueryWrapper<YxUser>()
                    .eq(YxUser::getUid, couponUserDTO.getUid()));
            if (yxUser != null) {
                couponUserDTO.setNickname(yxUser.getNickname());
                couponUserDTO.setUsername(yxUser.getUsername());
                couponUserDTO.setShopName(yxUser.getShopName());
            }

        }

        map.put("content", storeOrderDTOS);
        map.put("totalElements", page.getTotal());
        return map;
    }


    @Override
    //@Cacheable
    public List<YxStoreCouponUser> queryAll(YxStoreCouponUserQueryCriteria criteria) {
        return baseMapper.selectList(QueryHelpPlus.getPredicate(YxStoreCouponUser.class, criteria));
    }

    @Override
    public void download(List<YxStoreCouponUserDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (YxStoreCouponUserDto yxStoreCouponUser : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("兑换的项目id", yxStoreCouponUser.getCid());
            map.put("优惠券所属用户", yxStoreCouponUser.getUid());
            map.put("优惠券名称", yxStoreCouponUser.getCouponTitle());
            map.put("优惠券的面值", yxStoreCouponUser.getCouponPrice());
            map.put("最低消费多少金额可用优惠券", yxStoreCouponUser.getUseMinPrice());
            map.put("优惠券创建时间", yxStoreCouponUser.getAddTime());
            map.put("优惠券结束时间", yxStoreCouponUser.getEndTime());
            map.put("使用时间", yxStoreCouponUser.getUseTime());
            map.put("获取方式", yxStoreCouponUser.getType());
            map.put("状态（0：未使用，1：已使用, 2:已过期）", yxStoreCouponUser.getStatus());
            map.put("是否有效", yxStoreCouponUser.getIsFail());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public Map<String, Object> getUserPCCoupon(Long uid, int page, int limit, Integer type) {
        Page<YxStoreCouponUser> yxStoreCouponUserPage = new Page<>(page, limit);
        yxStoreCouponUserMapper.selectPage(yxStoreCouponUserPage, Wrappers.<YxStoreCouponUser>lambdaQuery()
                .eq(YxStoreCouponUser::getUid, uid).eq(YxStoreCouponUser::getStatus, type));

        List<YxStoreCouponUserQueryVo> storeCouponUserQueryVoList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (YxStoreCouponUser couponUser : yxStoreCouponUserPage.getRecords()) {
            YxStoreCouponUserQueryVo queryVo = generator.convert(couponUser, YxStoreCouponUserQueryVo.class);
            if (couponUser.getIsFail() == 1) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已失效");
            } else if (couponUser.getStatus() == 1) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已使用");
            } else if (couponUser.getStatus() == 2) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已过期");
            } else if (couponUser.getCreateTime().getTime() > nowTime || couponUser.getEndTime().getTime() < nowTime) {
                queryVo.set_type(CouponEnum.USE_0.getValue());
                queryVo.set_msg("已过期");
            } else {
                queryVo.set_type(CouponEnum.USE_1.getValue());
                queryVo.set_msg("可使用");
            }

            storeCouponUserQueryVoList.add(queryVo);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("list", storeCouponUserQueryVoList);
        map.put("total", yxStoreCouponUserPage.getTotal());
        map.put("totalPage", yxStoreCouponUserPage.getPages());
        return map;
    }
}
